<!--********************************************************************
* Copyright© 2000 - 2025 SuperMap Software Co.Ltd. All rights reserved.
*********************************************************************-->
<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title data-i18n="resources.title_singleObjectQueryJobService"></title>
    <script type="text/javascript" src="../../dist/ol/include-ol.js"></script>
    <style>
        .ol-popup {
            position: absolute;
            top: 10px;
            right: 10px;
        }

        .ol-draw {
            position: absolute;
            top: 70px;
            left: 5px;
            display: none;
        }
    </style>
</head>

<body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%;position: absolute;top: 0;">
    <div id="map" style="width: 100%;height:100%"></div>
    <div id="popup" class="ol-popup" style='width:350px'>
        <div class="panel panel-default">
            <div class='panel-heading'>
                <h3 class='panel-title' data-i18n="resources.title_singleObjectQueryJobService"></h3>
            </div>
            <div class='panel-body'>
                <div class='input-group'>
                    <span class='input-group-addon'><span data-i18n="resources.text_sourceDataset"></span><span
                            data-i18n="[title]resources.text_requiredField" style="color: red;"> * </span>　</span>
                    <input id='datasetName' type='text' class='form-control' value='samples_processing_newyorkZone_R' /></div>
                <p></p>
                <div class='input-group'>
                    <span class='input-group-addon'><span data-i18n="resources.text_queryObjectMode"></span><span
                            data-i18n="[title]resources.text_requiredField" style="color: red;"> * </span></span>
                    <select class='form-control' id='queryMode' name='queryMode'>
                        <option value='DATASET' selected data-i18n="resources.text_dataset"></option>
                        <option value='GEOJSON'>Geojson</option>
                    </select>
                </div>
                <p></p>
                <div class='input-group' id='datasetDiv'>
                    <span class='input-group-addon'><span data-i18n="resources.text_queryObjectDataset"></span><span
                            data-i18n="[title]resources.text_requiredField" style="color: red;"> * </span></span>
                    <input id='datasetQuery' type='text' class='form-control' value='samples_processing_singleRegion_R' />
                </div>
                <p></p>
                <div class='input-group'>
                    <span class='input-group-addon'><span data-i18n="resources.text_boundsQueryMode"></span><span
                            data-i18n="[title]resources.text_requiredField" style="color: red;"> * </span></span>
                    <select class='form-control' id='mode' name='mode'>
                        <option value='CONTAIN' data-i18n="resources.text_contain"></option>
                        <option value='CROSS' data-i18n="resources.text_cross"></option>
                        <option value='DISJOINT' data-i18n="resources.text_disjoint"></option>
                        <option value='IDENTITY' data-i18n="resources.text_Identity"></option>
                        <option value='INTERSECT' selected data-i18n="resources.text_intersect"></option>
                        <option value='OVERLAP' data-i18n="resources.text_overlap"></option>
                        <option value='TOUCH' data-i18n="resources.text_touch"></option>
                        <option value='WITHIN' data-i18n="resources.text_within"></option>
                    </select>
                </div>
                <p></p>
                <div align='right'>
                    <input type='button' id='btn' class='btn btn-primary' data-i18n="[value]resources.btn_query" />
                </div>
            </div>
        </div>
    </div>
    <div id="draw" class="ol-draw">
        <div class="btn-group" role="group" aria-label="...">
            <button id="drawPolygon" value='Polygon' type="button" class="btn btn-default" data-i18n="resources.text_input_value_drawPolygon"></button>
            <br>
            <button id="clear" value='Clear' type="button" class="btn btn-default" data-i18n="resources.text_input_value_clear"></button>
        </div>
    </div>
    <script type="text/javascript" include="bootstrap,jquery,widgets" src="../js/include-web.js"></script>
    <script type="text/javascript">
        var host = window.isLocal ? window.server : "https://iserver.supermap.io";
        var layer, draw,
            processingsUrl = host + "/iserver/services/distributedanalyst/rest/v1/jobs",
            mapURL = host + "/iserver/services/map-world/rest/maps/World",
            map = new ol.Map({
                target: 'map',
                // ol v7版本用法为ol.control.defaults.defaults； v6版本以下用法为ol.control.defaults
                controls: ol.control.defaults.defaults({
                        attributionOptions: {
                            collapsed: false
                        }
                    })
                    .extend([new ol.supermap.control.Logo({ link: "https://iclient.supermap.io" })]),
                view: new ol.View({
                    center: [-73.95, 40.75],
                    zoom: 12,
                    projection: 'EPSG:4326',
                    multiWorld: true
                })
            });
        map.addLayer(new ol.layer.Tile({
            source: new ol.source.TileSuperMapRest({
                url: mapURL,
            }),
        }));
        var processingService = new ol.supermap.ProcessingService(processingsUrl, {
            withCredentials: window.isLocal
        });
        var source = new ol.source.Vector({
            wrapX: false
        });
        var vector = new ol.layer.Vector({
            source: source
        });
        map.addLayer(vector);
        var info = new ol.control.Control({
            element: document.getElementById('draw')
        });
        info.setMap(map);
        map.addControl(info);

        $("#queryMode").change(function () {
            var queryMode = $(this).val();
            if (queryMode === "DATASET") {
                $('#draw').hide();
                $('#datasetDiv').show();
            }
            if (queryMode === "GEOJSON") {
                $('#draw').show();
                $('#datasetDiv').hide();
            }
        });

        var buttons = $('.btn-group').children();
        buttons.map(function (key) {
            var value = buttons[key].value;
            if (value === 'Clear') {
                $(buttons[key]).on('click', function () {
                    clearInteraction();
                    source.clear();
                    draw = undefined;
                });
                return;
            }
            $(buttons[key]).on('click', function () {
                clearInteraction();
                source.clear();
                draw = new ol.interaction.Draw({
                    source: source,
                    type: buttons[key].value,
                    snapTolerance: 20
                });
                draw.on('drawend', function () {
                    map.removeInteraction(draw);
                });
                map.addInteraction(draw);
            });
        });

        function clearInteraction() {
            if (draw) {
                map.removeInteraction(draw);
            }
        }

        initForm();
        bindClick();

        function initForm() {
            var info = new ol.control.Control({
                element: popup
            });
            info.setMap(map);
            map.addControl(info);
        }

        function bindClick() {
            $('#btn').on('click', function () {
                if ($('#msg_container')[0]) {
                    $('#msg_container').remove();
                }
                widgets.loader.showLoader();
                if (map && layer) {
                    map.removeLayer(layer);
                }
                queryJobs();
            });
        }

        ol.supermap.SecurityManager.registerToken(processingsUrl, window.exampleToken);

        function queryJobs() {
            var points, queryParam;
            if ($("#queryMode").val() === "DATASET") {
                queryParam = $('#datasetQuery').val();
                points = [];
            }
            if ($("#queryMode").val() === "GEOJSON") {
                if (!draw) {
                    widgets.loader.removeLoader();
                    widgets.alert.showAlert(resources.msg_createFailed + "<br>" + resources.msg_drawQueryBounds, false);
                    return;
                } else {
                    points = draw.a[0];
                    queryParam = "";
                }
            }
            var singleObjectQueryJobsParameter = new ol.supermap.SingleObjectQueryJobsParameter({
                datasetName: $('#datasetName').val(),
                datasetQuery: queryParam,
                geometryQuery: points,
                mode: $('#mode option:selected').attr('value')
            });
            processingService.addQueryJob(singleObjectQueryJobsParameter).then(function (serviceResult) {
                if (serviceResult.error) {
                    widgets.loader.removeLoader();
                    var errorMsg = serviceResult.error.errorMsg || "code: " + serviceResult.error.code;
                    widgets.alert.showAlert(resources.msg_createFailed + "<br>" + errorMsg, false);
                    return;
                }
                serviceResult.result.setting.serviceInfo.targetServiceInfos.map(function (info) {
                    if (info.serviceType === 'RESTMAP') {
                        ol.supermap.FetchRequest.get(info.serviceAddress + '/maps').then(function (
                            response) {
                            return response.json();
                        }).then(function (result) {
                            var mapUrl = result[0].path;
                            new ol.supermap.MapService(mapUrl).getMapInfo().then(function (mapInfo) {
                                layer = new ol.layer.Tile({
                                    source: new ol.source.ImageSuperMapRest(ol.source
                                        .ImageSuperMapRest.optionsFromMapJSON(
                                            mapUrl, mapInfo.result))
                                });
                                map.addLayer(layer);
                                widgets.loader.removeLoader();
                            });
                        });
                    }
                });
            });
        }
    </script>
</body>

</html>